package com.example.easyexcel.exports.simple_export.ReadMe.demos;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.example.easyexcel.exports.simple_export.SimpleExporter;
import com.example.easyexcel.exports.simple_export.ReadMe.demos.dtos.DemoData;
import com.example.easyexcel.exports.simple_export.ReadMe.demos.dtos.DemoDataPackager;
import com.example.easyexcel.exports.simple_export.ReadMe.demos.dtos.ListDto;
import com.example.easyexcel.exports.simple_export.ReadMe.demos.dtos.PageDto;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * EasyExcel 导出 测试
 */
@RestController
@RequestMapping("/simple/export/demo")
public class SimpleExportController {
    public static void main(String[] args) {
////        ExpressionParser parser = new SpelExpressionParser();
////        Expression expression = parser.parseExpression("('Hello' + ' freebuf').concat(#end)");
////        EvaluationContext context = new StandardEvaluationContext();
////        context.setVariable("end", "!");
////        System.out.println(expression.getValue(context));
//
//        String tmp = "{\"sampleInfoConfig\":[{\"name\":\"aaa\",\"type\":0,\"isRequired\":false,\"selectOption\":[{\"label\":\"a\",\"value\":0},{\"label\":\"s\",\"value\":1},{\"label\":\"d\",\"value\":2}],\"key\":\"dss\",\"prop\":\"dss\"}],\"sampleInfoData\":{\"dss\":\"a\"},\"contrastInfo\":{\"aaa#dss\":\"a\"}}";
//
//        System.out.println( JSONUtil.isTypeJSON(tmp) );
//
//        ExportDataDTO dto = new ExportDataDTO();
//        dto.setLink(new HyperlinkVo(){{  setName("aaaaaaaaaaaaaaaa") ; }});
//        System.out.println( BeanUtil.getProperty(dto,"link.name").toString() );
//
//        System.out.println("------------------------------");
//        System.out.println( getVal(tmp,"sampleInfoConfig.name[0]") );
//        System.out.println( getVal(tmp,"sampleInfoConfig.key") );
//        System.out.println( getVal(tmp,"sampleInfoConfig.selectOption.label") );
//        System.out.println( getVal(tmp,"contrastInfo") );
//        System.out.println( getVal(tmp,"contrastInfo.aaa#dss") );
//
//        System.out.println( getVal(dto,"link.name") );
//
//        System.out.println( getVal(null,"link.name") );
//        System.out.println( getVal(dto,"link.name44") );
//        System.out.println( getVal(dto,null) );

    }

    public static Object getVal(Object obj, String path) {
        try {
            return BeanUtil.getProperty(obj, path);
        } catch (Exception e) {
            JSON json = JSONUtil.parse(obj);
            return JSONUtil.getByPath(json, path);
        }
    }

    /**
     * 不带分页的接口
     */
    @SimpleExporter
    @PostMapping(value = "list1/{p1}")
    public List list1(
            @PathVariable("p1") Integer p1, @RequestParam(value = "p2", required = false) Integer p2
            , @RequestBody ListDto body
    ) {
        List<DemoData> dataList = DemoData.getDemoDatas();
        return dataList;
    }

    /**
     * 不带分页的接口
     */
    @SimpleExporter
    @PostMapping(value = "list2/{p1}")
    public DemoDataPackager list2(
            @PathVariable("p1") Integer p1, @RequestParam(value = "p2", required = false) Integer p2
            , @RequestBody ListDto body
    ) {
        List<DemoData> dataList = DemoData.getDemoDatas();
        return new DemoDataPackager(dataList);
    }

    /**
     * 带分页的接口（分页在url）
     */
    @SimpleExporter
    @PostMapping(value = "page1/{pageNum}")
    public PageDto page1(
            @PathVariable("pageNum") Integer pageNum
            , @RequestParam(value = "p2", required = false) Integer p2
//            ,@RequestParam(value = "pageNum",required = true) Integer pageNum
            , @RequestParam(value = "pageSize", required = true) Integer pageSize
    ) {
        List<DemoData> dataList = DemoData.getDemoDatas();

        PageDto page = new PageDto();
        page.setPageNum(pageNum);
        page.setPageSize(pageSize);
        page.setTotal(dataList.size());
        page.setDatas(dataList);
        return page;
    }

    /**
     * 带分页的接口（分页在请求体）
     */
    @SimpleExporter
    @PostMapping(value = "page2/{p1}")
    public PageDto page2(
            @PathVariable("p1") Integer p1
            , @RequestParam(value = "p2", required = false) Integer p2
            , @RequestBody PageDto exportDataDTO
    ) {
        List<DemoData> dataList = DemoData.getDemoDatas();

        PageDto page = new PageDto();
        page.setPageNum(exportDataDTO.getPageNum());
        page.setPageSize(exportDataDTO.getPageSize());
        page.setTotal(dataList.size());
        page.setDatas(dataList);
        return page;
    }


}
















